package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ecs.xml.XML;

import com.google.code.facebookapi.FacebookJaxbRestClient;
import com.google.code.facebookapi.FacebookWebappHelper;
import com.google.code.facebookapi.Permission;

import facebook.FacebookUserFilter;

public class GetPermissions extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String api_key = "672c814ee3de3c59ed6434a6bbf190d8";
	private static final String secret = "65246ca3141547f064aa1f7d3df217cc";
	private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	HttpSession session = request.getSession(true);

    	FacebookJaxbRestClient userClient = FacebookUserFilter.getUserClientJaxb(session);
    	if (userClient == null) {
    		userClient = new FacebookJaxbRestClient(api_key, secret);
    		session.setAttribute(FACEBOOK_USER_CLIENT, userClient);
    	}
    	
        FacebookWebappHelper<?> facebook = new FacebookWebappHelper<Object>(
                request, response, api_key, secret, userClient);
    	
    	XML status = new XML("status");
    	
		if (!facebook.isLogin()) {
			status.addElement("no-login");
		} else {
			status.addElement("good-to-go");
		}
		
		response.getWriter().write(status.toString());
	}
}
